
require('wy-Prototype')

if (!window['swan']) {
    window.CC_BAIDUGAME = 0
}
if (!window['tt']) {
    window.CC_TOUTIAOGAME = 0
}

cc.Class({
    extends:require('wy-Component'),

    properties: {
        uiMgr : require('UIManager'),
        ejMgr : require('EjectManager'),
        gameLoop :require('GameLoop'),
        config : cc.RawAsset,
        resJson : cc.RawAsset
    },

    onLoad () {
        // cc.sys.localStorage.clear();
        if(window.global == null){
            window.global = {}
        }
        global.Config = require('wy-Config')
        global.Common = require('wy-Common');
        global.Audio = require('wy-Audio')
        global.Http = require('wy-Http')
        // global.Platform = require('wy-WeChat');
        global.DataMgr = require("wy-DataManager");
        global.NodePool = require('NodePoolManager')
        global.ChatMsgQueue = [];
        global.Loader = require('wy-Loader')
        global.UIMgr = this.uiMgr;
        global.EJMgr = this.ejMgr;
        global.Event = require('wy-EventEmiter');
        global.GameLoop = this.gameLoop;
        global.DataConfig = {} //游戏的数据配置
        global.PathConfig = {}; //路径数据
        global.ParticleConfig = {}; //粒子数据
        global.SkillConfig = {}; //技能配置数据
        window.CCAsync = require('CCAsync')
        global.UI = require('wy-UI')
        global.Enum = require('Enum')
        global.BannerAD = require('wy-BannerAD')
        global.Data = {};
        global.planeId = null;//飞机
        global.gates = null;//关卡
        global.unlock=null;//最大关卡
        global.gaming=false;//是否正在游戏
        global.bossDieCount=0;//无尽模式打的boss个数
        global.WechatShare = require('WechatShare')
        window.SoundMgr = require('SoundMgr')
        global.ErrorLog = require('ErrorLog')
        global.NullFunction = function(){}
        var Platform = this.initPlatform();
        if (!global.Platform) {
            global.Platform = Platform;
            global.Platform.init();
        }
        
        global.Loader.init();
        SoundMgr.init();


        cc.find('Loadding/mask',this.node).active=false
        // global.upgrade={
        //     "0":[1,2,3,4,5],
        //     "1":[20,30,40,50,60],
        //     "2":[1,2,3],
        //     "3":[50,100,150,200,250],
        //     "4":[5000,10000,20000,40000,80000]
        // };
    },

    initPlatform() {
        var platform = null;
        if (CC_BAIDUGAME) {
            platform = require('wy-BaiDu');
        } else if (CC_TOUTIAOGAME) {
            platform = require('wy-TouTiao');
        } else if (CC_WECHATGAME) {
            platform = require('wy-WeChat');
        } else if (cc.sys.platform == cc.sys.QQ_PLAY) {
            platform = require('wy-QQPlay');
        } else {
            platform = require('wy-WebTest');
        }
        return new platform();
    },

    start () {
        // var data = cc.sys.localStorage.getItem('globalData');
        // if(!data){
        //     global.Data = {
        //         coin : 5000,//金币
        //         mainLevel : 0,//主武器等级
        //         secondLevel : 0,//暴走等级
        //         empLevel : 0,//初始能量
        //         bombLevel : 0,//必杀技能等级
        //         plane:[1,0,0,0],//飞机是否解锁
        //         level:[1,0,0,0,0,0],//关卡是否解锁
        //         plane.gateFirst:[0,0,0,0,0,0],//关卡是否第一次通过
        //         videoTimes:'2018-11-14/0',//观看视频次数   
        //         shareTimes:'2018-11-14/0'//分享次数
        //     }
        // } else {
        //     if(data=='[object Object]'){
        //         this.pushJSON()
        //     }else{
        //         global.Data = JSON.parse(data);
        //     }
        // }
        // global.Data.coin = 100
        
        CC_WECHATGAME && wx.showLoading({title:'加载中',mask:true})
        var self = this;
        cc.loader.load(this.resJson,function(err,res){
            global.Resources = res.json;
            cc.find('Canvas/UI/Login').active = true;
            // global.Loader.loadResources('Main',function(){
            //     CC_WECHATGAME && wx.hideLoading()
            //     global.UIMgr.addUI('main/Login');
            // })
        })
        cc.game.on(cc.game.EVENT_HIDE, function () {
            cc.log('暂停');
            cc.director.pause();
        });
        cc.game.on(cc.game.EVENT_SHOW, function () {
            cc.log('恢复');
            cc.director.resume();
            if(global.Data._id){
                global.Data.isNewUser = global.Common.calcDetailDay(new Date(global.Data.registerTimeTick),new Date()) == 0;
            }
        });
        
        if(CC_WECHATGAME){
            wx.RankKey = 'score',
            wx.LevelKey = 'level'
        }
        // cc.game.setFrameRate(40)
    },

    pushJSON(){
        // global.Data={
        //     coin : 0,//金币
        //     mainLevel : 0,//主武器等级
        //     secondLevel : 0,//副武器等级
        //     empLevel : 0,//初始能量
        //     bombLevel : 0,//必杀技能等级
        //     plane:[1,0,0,0],//飞机是否解锁
        //     level:[1,0,0,0,0,0],//关卡是否解锁
        //     plane.gateFirst:[0,0,0,0,0,0]//关卡是否第一次通过
        // }
        // cc.sys.localStorage.setItem('globalData',JSON.stringify(global.Data))
    },

    

    onBtnStart(){
        
    },

    update (dt) {
        //cc.log('darwcall:',cc.renderer.drawCalls)
    },
});
